package com.ruoyi.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.domain.Leader;
import com.ruoyi.mapper.LeaderMapper;
import com.ruoyi.service.ILeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.domain.Station;
import com.ruoyi.mapper.WaterMapper;
import com.ruoyi.domain.Water;
import com.ruoyi.service.IWaterService;

/**
 * 水情管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-18
 */
@Service
public class WaterServiceImpl extends ServiceImpl<WaterMapper, Water>  implements IWaterService {
    @Autowired
    private WaterMapper waterMapper;

    /**
     * 查询水情管理
     *
     * @param id 水情管理主键
     * @return 水情管理
     */
    @Override
    public Water selectWaterById(Long id) {
        return waterMapper.selectWaterById(id);
    }

    /**
     * 查询水情管理列表
     *
     * @param water 水情管理
     * @return 水情管理
     */
    @Override
    public List<Water> selectWaterList(Water water) {
        return waterMapper.selectWaterList(water);
    }

    /**
     * 新增水情管理
     *
     * @param water 水情管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWater(Water water) {
        int rows = waterMapper.insertWater(water);
        insertStation(water);
        return rows;
    }

    /**
     * 修改水情管理
     *
     * @param water 水情管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWater(Water water) {
        waterMapper.deleteStationByWaterId(water.getId());
        insertStation(water);
        return waterMapper.updateWater(water);
    }

    /**
     * 批量删除水情管理
     *
     * @param ids 需要删除的水情管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWaterByIds(Long[] ids) {
        waterMapper.deleteStationByWaterIds(ids);
        return waterMapper.deleteWaterByIds(ids);
    }

    /**
     * 删除水情管理信息
     *
     * @param id 水情管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWaterById(Long id) {
        waterMapper.deleteStationByWaterId(id);
        return waterMapper.deleteWaterById(id);
    }

    /**
     * 新增站台数据信息
     *
     * @param water 水情管理对象
     */
    public void insertStation(Water water) {
        List<Station> stationList = water.getStationList();
        Long id = water.getId();
        if (StringUtils.isNotNull(stationList)) {
            List<Station> list = new ArrayList<Station>();
            for (Station station : stationList) {
//                station.setWaterId(id); 无需设置
                if (station.getWaterId() == null) station.setWaterId(id);
                list.add(station);
            }
            if (list.size() > 0) {
                waterMapper.batchStation(list);
            }
        }
    }
}
